package com.coach.control;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coach.entity.ClassDTO;
import com.coach.entity.Class;
import com.coach.entity.ClassToCoach;
import com.coach.entity.ItemDelivery;
import com.coach.entity.SAttendance;
import com.coach.entity.User;
import com.coach.service.ClassService;
import com.coach.service.ItemDeliveryService;
import com.coach.util.BaseResponse;

@Controller
@RequestMapping("/class")
public class ClassController {
   
	@Autowired
	private ClassService classService;
	@Autowired
	private ItemDeliveryService itemDeliveryService;
	
	@RequestMapping(value="/{id}",produces="application/json",method=RequestMethod.GET)
	@ResponseBody
	public BaseResponse<ClassDTO> getClassById(@PathVariable String id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		int cid=Integer.parseInt(id);
		if(cid>0){
			return classService.getClassById(cid);
		}else {
			return null;
		}
	}
	
	@RequestMapping(value="/list",produces="application/json",method=RequestMethod.GET)
	@ResponseBody
	public BaseResponse<ClassDTO> getClassList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String province=request.getParameter("province");
		String city=request.getParameter("city");
		String area=request.getParameter("area");
        String name=request.getParameter("name");		       
		Map<String,Object> map=new HashedMap();
		map.put("province",province);
		map.put("city", city);
		map.put("area", area);
		map.put("name", name);
		return classService.getClassList(map);
	}
	
	
	@RequestMapping(value="/userclass",produces="application/json",method=RequestMethod.POST)
	@ResponseBody
	public BaseResponse<Class> getClassListByUserId(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userId=request.getParameter("userId");		       
		Map<String,Object> map=new HashedMap();
        if(userId!=null) map.put("studentId", userId);
		return classService.getUserClassList(map);
	}
	
	@RequestMapping(value="/coachclass",produces="application/json",method=RequestMethod.POST)
	@ResponseBody
	public BaseResponse<Class> getClassListByCoachId(@RequestParam("coachId") int coachId) throws Exception{		       
		Map<String,Object> map=new HashedMap();
        map.put("coachId", coachId);
		return classService.getCoachClassList(map);
	}
	
	@RequestMapping(value="/getItemDelivery",produces="application/json",method=RequestMethod.POST)  //查询学生及签到状态
	@ResponseBody
	public BaseResponse<User> getClassItemDeliveryStudents(@RequestParam("classId") int classId)throws Exception{
	   Map<String,Object> map=new HashedMap();
	   map.put("classId", classId);
	   return itemDeliveryService.getItemDeliveryUserList(map);	
	}
	
	@RequestMapping(value="/checkauthority",produces="application/json",method=RequestMethod.POST)  //判断该教练是否是该班级的代课教练
	@ResponseBody
	public BaseResponse<ClassToCoach> checkAuthority(@RequestParam("classId") int classId,@RequestParam("userId") int userId)throws Exception{
	   Map<String,Object> map=new HashedMap();
	   map.put("classId", classId);
	   map.put("userId",userId);
	   return classService.checkClassToCoach(map);	
	}
	
	@RequestMapping(value="/saveitemdelivery",produces="application/json",method=RequestMethod.POST)  //保存学生签到状态
	@ResponseBody
	public BaseResponse<Integer> saveCourseStudents(@RequestParam("studentIds") String studentsIds,@RequestParam("classId") int classId)throws Exception{
          BaseResponse<Integer> se=new BaseResponse<Integer>();
         String[] lists=studentsIds.split(";");
        for(int i=0;i<lists.length;i++){
        	String[] nums=lists[i].split("#");
        	if(nums.length==3){
        		int n1=Integer.parseInt(nums[0]); //签到表id
        		int n2=Integer.parseInt(nums[1]); //对应user表id
        		int n3=Integer.parseInt(nums[2]); //状态是否改变,0不签到,1签到(若签到表id存在，为1时不处理，为0时将其删除)
        		if(n1>0&&n3==0){ //删除
        			itemDeliveryService.deleteItemDelivery(n1);
        		}else if(n1==0&&n3==1){ //新增
        			ItemDelivery sa=new ItemDelivery();
        			sa.setClassId(classId);sa.setUserId(n2);
        			itemDeliveryService.addItemDelivery(sa);
        		}
             se.setSuccess(true);
        	}else se.setSuccess(false);
        }
        
        return  se;
	}
	
		
	@RequestMapping(value="/",produces="application/json",method=RequestMethod.POST)  //新增
	@ResponseBody
	public BaseResponse<Class> addClass(@RequestBody Class cls,BindingResult br)throws Exception{
		if(br.hasErrors()||cls==null){
			return null;
		}
		return classService.addClass(cls);		
	}
	
	@RequestMapping(value="/{id}",produces="application/json",method=RequestMethod.PUT) //更新
	@ResponseBody
	public BaseResponse<Class> updateClass(@PathVariable String id,@RequestBody Class cls,BindingResult br)throws Exception{
		//@Validated对Class数据进行校验
		if(br.hasErrors()){
			return null;
		}
		int cid=Integer.valueOf(id);
		if(cid>0){
			return classService.updateClass(cls);
		}else{
			return null;
		}
		
	}
	
	@RequestMapping(value="/{id}",produces="application/json",method=RequestMethod.DELETE)
	@ResponseBody
	public BaseResponse<Class> deleteClass(@PathVariable String id,@Validated Class cls,BindingResult br)throws Exception{
		if(br.hasErrors()){
			return null;
		}
		int cid=Integer.valueOf(id);
		if(cid>0){
			return classService.deleteClass(cls);
		}else{
			return null;
		}
	}
	
	
	
	
	
}
